package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.MemberLevel;
import com.yaowk.api.mall.clothing.service.IMemberLevelService;
import com.yaowk.api.mall.clothing.web.MemberLevelModel;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.dao.MemberLevelDao;
import com.yaowk.service.mall.clothing.entity.CmMemberLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberLevelServiceImpl extends BaseServiceImpl<MemberLevel, CmMemberLevel, Integer> implements IMemberLevelService {

    @Autowired
    private MemberLevelDao dao;

    @Override
    public PageResp<MemberLevelModel> findAllOfWeb(PageReq pageReq) {
        // 查询列表
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize());
        Page<CmMemberLevel> page = getDao().findAll(pageRequest);

        // 构造返回
        PageResp<MemberLevelModel> pageResp = new PageResp<>();
        pageResp.setPageNumber(page.getNumber() + 1);
        pageResp.setPageSize(page.getSize());
        pageResp.setTotalPage(page.getTotalPages());
        pageResp.setTotalRow(page.getTotalElements());
        pageResp.setList(new ArrayList<>());

        List<MemberLevelModel> memberGroupModels = pageResp.getList();
        for (CmMemberLevel memberLevel : page.getContent()) {
            MemberLevelModel memberLevelModel = new MemberLevelModel();
            memberLevelModel.setId(memberLevel.getId());
            memberLevelModel.setName(memberLevel.getName());
            memberLevelModel.setUpdatable(memberLevel.getUpdatable());
            memberLevelModel.setCondition(memberLevel.getCondition());
            memberLevelModel.setDiscount(memberLevel.getDiscount());
            memberLevelModel.setLevel(memberLevel.getLevel());
            memberLevelModel.setStatus(memberLevel.getStatus());
            memberLevelModel.setMemberNum(memberLevel.getMembers().size());

            memberGroupModels.add(memberLevelModel);
        }

        return pageResp;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmMemberLevel model2Entity(MemberLevel model) {
        CmMemberLevel memberLevel = null;
        if (isNotEmpty(model)) {
            memberLevel = new CmMemberLevel();
            memberLevel.setCondition(model.getCondition());
            memberLevel.setDiscount(model.getDiscount());
            memberLevel.setLevel(model.getLevel());
            memberLevel.setName(model.getName());
            memberLevel.setStatus(model.getStatus());
            memberLevel.setUpdatable(model.getUpdatable());
            memberLevel.setId(model.getId());
        }
        return memberLevel;
    }

    @Override
    protected MemberLevel entity2model(CmMemberLevel entity) {
        MemberLevel memberLevel = null;
        if (isNotEmpty(entity)) {
            memberLevel = new MemberLevel();
            memberLevel.setCondition(entity.getCondition());
            memberLevel.setDiscount(entity.getDiscount());
            memberLevel.setLevel(entity.getLevel());
            memberLevel.setName(entity.getName());
            memberLevel.setStatus(entity.getStatus());
            memberLevel.setUpdatable(entity.getUpdatable());
            memberLevel.setId(entity.getId());
        }
        return memberLevel;
    }

    @Override
    public MemberLevel save(MemberLevel model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            CmMemberLevel cmMemberLevel = dao.findOne(model.getId());
            if (isNotEmpty(model.getCondition())) {
                cmMemberLevel.setCondition(model.getCondition());
            }
            if (isNotEmpty(model.getDiscount()))
                cmMemberLevel.setDiscount(model.getDiscount());
            if (isNotEmpty(model.getLevel()))
                cmMemberLevel.setLevel(model.getLevel());
            if (isNotEmpty(model.getName()))
                cmMemberLevel.setName(model.getName());
            if (isNotEmpty(model.getStatus()))
                cmMemberLevel.setStatus(model.getStatus());
            model = entity2model(dao.save(cmMemberLevel));
        }
        return model;
    }
}
